package com.google.coffeeshop.gui.desktop.employee;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import vn.bang.utility.DateUtil;
import vn.bang.utility.FileUtil;
import vn.bang.utility.StringUtil;

import com.google.coffeeshop.business.CodeGenerationBusiness;
import com.google.coffeeshop.business.ManagerBusiness;
import com.google.coffeeshop.entity.EmployeeDTO;
import com.google.coffeeshop.gui.desktop.DatePicker;
import com.google.coffeeshop.gui.desktop.ObservingTextField;
import com.google.coffeeshop.gui.desktop.SwingFactory;
import com.google.coffeeshop.gui.desktop.selfcomponent.ImagePanel;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class AddEditEmployeeFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private JTextField txtCode;
	private JTextField txtName;
	private ImagePanel pnlImage;
	private ObservingTextField txtBornDate;
	private ObservingTextField txtBeginDate;
	private String image_filename = SwingFactory.NO_IMAGE_NAME;
	private Long employeeId = null;
	private EmployeePanel employeePanel;
	/**
	 * Create the panel.
	 */
	public AddEditEmployeeFrame(EmployeePanel employeePanel, EmployeeDTO employee) {
		this.employeePanel = employeePanel;
		employeeId = employee != null ? employee.getId() : null;
		setSize(600, 335);
		
		getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(39dlu;default)"),
				ColumnSpec.decode("max(7dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(121dlu;default)"),
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(31dlu;default)"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblAddNewEmployee = new JLabel();
		String text = employee == null ? "Add New Employee" : "Edit Employee";
		lblAddNewEmployee.setText(text);
		lblAddNewEmployee.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblAddNewEmployee.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().add(lblAddNewEmployee, "2, 2, 9, 1, center, default");
		
		pnlImage = SwingFactory.createImagePanel(Boolean.TRUE);
		pnlImage.setImage(FileUtil.getRelativePathFromProjectPath("employee_images\\" + SwingFactory.NO_IMAGE_NAME));
		getContentPane().add(pnlImage, "2, 4, 1, 11, fill, fill");
		
		JLabel lblCode = new JLabel("Code");
		getContentPane().add(lblCode, "4, 4, right, default");
		
		txtCode = new JTextField();
		txtCode.setText(CodeGenerationBusiness.getEmployeeCode());
		getContentPane().add(txtCode, "7, 4, fill, default");
		txtCode.setColumns(10);
		
		JLabel lblName = new JLabel("Name");
		getContentPane().add(lblName, "4, 6, right, default");
		
		txtName = new JTextField();
		getContentPane().add(txtName, "7, 6, fill, default");
		txtName.setColumns(10);
		
		JLabel lblBornDate = new JLabel("Born Date");
		getContentPane().add(lblBornDate, "4, 10, right, default");
		
		txtBornDate = new ObservingTextField();
		getContentPane().add(txtBornDate, "7, 10, fill, default");
		txtBornDate.setEditable(Boolean.FALSE);
		txtBornDate.setColumns(10);
		txtBornDate.setText(DateUtil.toString(DateUtil.getNow()));
		
		JButton btnBornDate = new JButton("...");
		btnBornDate.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e){
				datePicker(txtBornDate);
			}
		});
		getContentPane().add(btnBornDate, "10, 10");
		
		JLabel lblBeginDate = new JLabel("Begin Date");
		getContentPane().add(lblBeginDate, "4, 12, right, default");
		
		txtBeginDate = new ObservingTextField();
		getContentPane().add(txtBeginDate, "7, 12, fill, default");
		txtBeginDate.setEditable(Boolean.FALSE);
		txtBeginDate.setColumns(10);
		txtBeginDate.setText(DateUtil.toString(DateUtil.getNow()));
		
		JButton btnBeginDate = new JButton("...");
		btnBeginDate.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				datePicker(txtBeginDate);
			}
		});
		getContentPane().add(btnBeginDate, "10, 12");
		
		JButton btnChooseImage = new JButton("Choose an image");
		btnChooseImage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openFileChooserDialog();
			}
		});
		getContentPane().add(btnChooseImage, "2, 16");
		
		JButton btnAdd = new JButton(employee == null ? "Add" : "Save");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateOrAddEmployee();
			}
		});
		getContentPane().add(btnAdd, "2, 20, 6, 1, fill, fill");
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeFrame();
			}
		});
		getContentPane().add(btnCancel, "10, 20, fill, fill");
		
		if (employee != null) {
			setEmployee(employee);
		}
	}
	
	private void refreshParentGUI() {
		employeePanel.refreshGUI();
	}
	
	private void setEmployee(EmployeeDTO employee) {
		txtCode.setText(employee.getCode());
		txtName.setText(employee.getFullName());
		txtBeginDate.setText(DateUtil.toString(employee.getBeginDate()));
		txtBornDate.setText(DateUtil.toString(employee.getBornDate()));
		image_filename = employee.getImageFilename();
		pnlImage.setImage(FileUtil.getRelativePathFromProjectPath("employee_images\\" + image_filename));
	}
	
	private EmployeeDTO getEmployeeDTO() {
		EmployeeDTO employee = new EmployeeDTO();
		employee.setId(employeeId);
		employee.setCode(txtCode.getText());
		employee.setFullName(txtName.getText());
		if (!StringUtil.isNullOrEmpty(txtBeginDate.getText())) {
			employee.setBeginDate(DateUtil.toDate(txtBeginDate.getText()));
		} else {
			employee.setBeginDate(DateUtil.getNow());
		}
		if (!StringUtil.isNullOrEmpty(txtBeginDate.getText())) {
			employee.setBornDate(DateUtil.toDate(txtBornDate.getText()));
		}
		employee.setEndDate(null);
		employee.setImageFilename(image_filename);
		return employee;
	}
	
	private void openFileChooserDialog() {
		JFileChooser chooser = new JFileChooser(FileUtil.getRelativePathFromProjectPath("employee_images\\"));
	    FileNameExtensionFilter filter = new FileNameExtensionFilter(
	        "JPG & PNG & GIF Images", "jpg", "png", "gif");
	    chooser.setFileFilter(filter);
	    int returnVal = chooser.showOpenDialog(this);
	    if(returnVal == JFileChooser.APPROVE_OPTION) {
	    	pnlImage.setImage(chooser.getSelectedFile().getAbsolutePath());
	    	image_filename = chooser.getSelectedFile().getName();
	    }
	}
	
	private void updateOrAddEmployee() {
		EmployeeDTO modifiedEmployee = getEmployeeDTO();
		ManagerBusiness.saveEmployee(modifiedEmployee);
		refreshParentGUI();
		disposeFrame();
	}
	
	private void disposeFrame() {
		this.dispose();
	}
	
	private Locale getLocale(String locate) {
		if(locate!=null && locate.length()>0)
			return new Locale(locate);
		else return Locale.FRANCE;
	}
	
	private void datePicker(ObservingTextField txtDate){
		String lang = null;
		final Locale loc = getLocale(lang);
		DatePicker dp = new DatePicker(txtDate, loc);
		Date selectedDate = dp.parseDate(txtDate.getText());
		dp.setSelectedDate(selectedDate);
		dp.start(txtDate);
	}
}
